package fr.mywiki.model.ejbAdapter.services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;

import fr.mywiki.model.ejbAdapter.EntityHomeCache;
import fr.mywiki.model.ejbAdapter.ServicesAdapter;
import fr.mywiki.model.ejbAdapter.entity.AccountLocal;
import fr.mywiki.model.ejbAdapter.entity.EntryLocal;
import fr.mywiki.model.ejbAdapter.entity.StructureLocal;
import fr.mywiki.model.ejbAdapter.entity.VersionLocal;
import fr.mywiki.model.ejbAdapter.services.business.FindAllServiceBusiness;

/**
 * This session bean deals with creating new entity beans.
 * 
 * @ejb.bean name = "FindAllService"
 *		display-name = "FindAllService"
 *		description = "FindAll Service"
 *		jndi-name = "ejb/MyWiki/FindAllService"
 *		remote-business-interface = "fr.mywiki.model.ejbAdapter.services.business.FindAllServiceBusiness"
 * 
 * @ejb.home
 * 		extends = "javax.ejb.EJBHome"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 *  
 * @ejb.interface
 * 		extends = "javax.ejb.EJBObject"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 * 
 * @author tlombard
 */
public class FindAllServiceBean extends ServicesAdapter implements FindAllServiceBusiness,SessionBean {
	/**
	 * The serial version UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Default create method
	 * @throws CreateException
	 * @ejb.create-method 
	 */
	public void ejbCreate() throws CreateException {
	}
	
	/**
	 * Finds and returns all the accounts of the database.
	 * 
	 * @return a <code>Collection</code> of <code>AccountLite</code> objects.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Collection findAllAccount() {
		ArrayList values = new ArrayList();
		try {
			Collection col = EntityHomeCache.getInstance().getAccountHome().findAll();
			Iterator it = col.iterator();
			while (it.hasNext()) {
				values.add(((AccountLocal) it.next()).getAccountLite());
			}
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return values;
	}
	
	/**
	 * Finds and returns all the entries of the database.
	 * 
	 * @return a <code>Collection</code> of <code>EntryLite</code> objects.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Collection findAllEntry() {
		ArrayList values = new ArrayList();
		try {
			Collection col = EntityHomeCache.getInstance().getEntryHome().findAll();
			Iterator it = col.iterator();
			while (it.hasNext()) {
				values.add(((EntryLocal) it.next()).getEntryLite());
			}
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return values;
	}
	
	/**
	 * Finds and returns all the structures of the database.
	 * 
	 * @return a <code>Collection</code> of <code>StructureLite</code> objects.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Collection findAllStructure() {
		ArrayList values = new ArrayList();
		try {
			Collection col = EntityHomeCache.getInstance().getStructureHome().findAll();
			Iterator it = col.iterator();
			while (it.hasNext()) {
				values.add(((StructureLocal) it.next()).getStructureLite());
			}
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return values;
	}
	
	/**
	 * Finds and returns all the versions of the database.
	 * 
	 * @return a <code>Collection</code> of <code>VersionLite</code> objects.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Collection findAllVersion() {
		ArrayList values = new ArrayList();
		try {
			Collection col = EntityHomeCache.getInstance().getVersionHome().findAll();
			Iterator it = col.iterator();
			while (it.hasNext()) {
				values.add(((VersionLocal) it.next()).getVersionLite());
			}
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return values;
	}
}
